package com.example.file_management.service;

import com.example.file_management.mapper.user.AuthMapper;
import com.example.file_management.mapper.user.GroupMapper;
import com.example.file_management.mapper.user.RoleMapper;
import com.example.file_management.pojo.user.Auth;
import com.example.file_management.pojo.user.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

import java.util.List;

@Service
public class GroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AuthMapper authMapper;// 注入RoleMapper用于校验角色

    // 获取所有组
    public List<Group> getAllGroups() {
        return groupMapper.findAllGroups();
    }
    // 根据角色ID获取对应的组的信息
//    public List<Group> getGroupsByRoleId(Long roleId) {
//        return groupMapper.findGroupsByRoleId(roleId);
//    }

    public List<Auth> getUsersByGroupId(Long id) {
        return authMapper.findUsersByGroupId(id);
    }
    // 创建组
    public String createGroup(Group group) {
        try {
            validateGroup(group, true); // 验证新组信息

            // 校验组名是否已存在
            if (groupMapper.isGroupNameExists(group.getGroupName())) {
                throw new IllegalArgumentException("组名已存在");
            }

            // 校验角色ID是否有效
            if (!roleMapper.isRoleExists(group.getRoleID())) {
                throw new IllegalArgumentException("角色ID无效");
            }

            // 设置当前时间为创建时间
            group.setCreatedAt(LocalDateTime.now());

            // 插入组信息
            groupMapper.insertGroup(group);
            return "组创建成功";
        } catch (DuplicateKeyException e) {
            return "创建失败: 组名已存在";
        } catch (IllegalArgumentException e) {
            return "创建失败: " + e.getMessage();
        } catch (Exception e) {
            return "创建失败: 系统错误，请稍后再试";
        }
    }

    // 更新组
    public String updateGroup(Group group) {
        try {
            validateGroup(group, false); // 验证组信息

            // 校验角色ID是否有效
            if (!roleMapper.isRoleExists(group.getRoleID())) {
                throw new IllegalArgumentException("角色ID无效");
            }

            // 更新组信息
            if (groupMapper.updateGroup(group) > 0) {
                return "组信息更新成功";
            } else {
                return "组信息更新失败";
            }
        } catch (IllegalArgumentException e) {
            return "更新失败: " + e.getMessage();
        } catch (Exception e) {
            return "更新失败: 系统错误，请稍后再试";
        }
    }

    // 删除组
    public String deleteGroup(Long groupId) {
        try {
            if (groupMapper.deleteGroup(groupId) > 0) {
                return "组删除成功";
            } else {
                return "组删除失败: 未找到该组";
            }
        } catch (Exception e) {
            return "删除失败: 系统错误，请稍后再试";
        }
    }

    // 统一的组信息校验方法
    private void validateGroup(Group group, boolean isNewGroup) {
        if (group.getGroupName() == null || group.getGroupName().isEmpty()) {
            throw new IllegalArgumentException("组名不能为空");
        }

        if (isNewGroup && (group.getParentGroupId() == null)) {
            throw new IllegalArgumentException("父组ID不能为空");
        }
    }
}